$(document).ready(function () {

    function getCookie(name) {
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }

    var csrftoken = getCookie('csrftoken');

    function csrfSafeMethod(method) {
        // these HTTP methods do not require CSRF protection
        return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
    }

    $.ajaxSetup({
        beforeSend: function (xhr, settings) {
            if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
                xhr.setRequestHeader("X-CSRFToken", csrftoken);
            }
        }
    }); // end of jquery csrf setting

    var timeout = 500;
    var closetimer = 0;
    var ddmenuitem = 0;

    function jsddm_open() {
        jsddm_canceltimer();
        jsddm_close();
        ddmenuitem = $(this).find('ul').eq(0).css('visibility', 'visible');
    }

    function jsddm_close() {
        if (ddmenuitem) ddmenuitem.css('visibility', 'hidden');
    }

    function jsddm_timer() {
        closetimer = window.setTimeout(jsddm_close, timeout);
    }

    function jsddm_canceltimer() {
        if (closetimer) {
            window.clearTimeout(closetimer);
            closetimer = null;
        }
    }

    $('#jsddm > li').bind('mouseover', jsddm_open);
    $('#jsddm > li').bind('mouseout', jsddm_timer);
    document.onclick = jsddm_close; // end of original js

    $("#my-menu").mmenu({});


    var str = location.href.toLowerCase();

    $("#jsddm li a").each(function () {
        if (str.indexOf(this.href.toLowerCase()) > -1) {
            $("a.active").removeClass("active");
            $(this).addClass("active");
        }

        else if (location.pathname === '/') {
            $("#index").addClass("active");
        }

    });    // 导航高亮

    $('.xzan').click(function () {
        var href_url = window.location.href;
        var arr = href_url.split("/");
        var url = arr[0] + "//" + arr[2] + "/api/v1/add_count/";
        var data = {pk: $(this).find('.hidden_pk').val()};
        $.ajax({
            dataType: "json",
            url: url,
            data: data,
            success: function (data) {
                location.reload();
            },
            crossDomain: false
        })
    }); // download count


    $('#faq_tab1').click(function() {
        $(this).css('color', '#2d2d2d').css('background-color', '#fff');
        $('#faq_tab2').css('color', '#868686').css('background-color', '#eee');
        $('#qatab01').css('display', 'block');
        $('#qatab02').css('display', 'none');
    });

    $('#faq_tab2').click(function() {
        $(this).css('color', '#2d2d2d').css('background-color', '#fff');
        $('#faq_tab1').css('color', '#868686').css('background-color', '#eee');
        $('#qatab02').css('display', 'block');
        $('#qatab01').css('display', 'none');
    }); // faq tab switch

});
